/**
 * 	write on 2005-11-15 13:22:23.
 */
package org.nanhill.xmlro.sro.model;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;

import org.nanhill.xmlro.AbstractSerializerFactory;
import org.nanhill.xmlro.Serializer;

/**
 * @author alin
 * 
 */
public class SroSerializerFactory extends AbstractSerializerFactory {

	protected Serializer objectSerializer;

	protected Serializer collectionSerializer;

	protected Serializer mapSerializer;

	protected Serializer utcDateSerializer;

	protected Serializer objectArraySerializer;

	protected Serializer booleanSerializer;

	protected Serializer byteSerializer;

	protected Serializer shortSerializer;

	protected Serializer integerSerializer;

	protected Serializer longSerializer;

	protected Serializer floatSerializer;

	protected Serializer doubleSerliazer;

	protected Serializer characterSerializer;

	protected Serializer stringSerializer;

	protected Serializer booleanArraySerializer;

	protected Serializer byteArraySerializer;

	protected Serializer shortArraySerializer;

	protected Serializer integerArraySerializer;

	protected Serializer longArraySerializer;

	protected Serializer floatArraySerializer;

	protected Serializer doubleArraySerializer;

	protected Serializer characterArraySerializer;

	protected Serializer stringArraySerializer;

	protected Serializer classSerializer;

	protected Serializer stringValueSerializer;

	public SroSerializerFactory() {
		init();
		initDispatcher();
	}

	protected void init() {
		this.objectSerializer = new JavaSerializer();
		this.collectionSerializer = new CollectionSerializer();
		this.mapSerializer = new MapSerializer();
		this.utcDateSerializer = new DateSerializer();
		this.objectArraySerializer = new ObjectArraySerializer();

		this.booleanSerializer = new BooleanSerializer();
		this.byteSerializer = new ByteSerializer();
		this.shortSerializer = new ShortSerializer();
		this.integerSerializer = new IntegerSerializer();
		this.longSerializer = new LongSerializer();
		this.floatSerializer = new FloatSerializer();
		this.doubleSerliazer = new DoubleSerializer();
		this.characterSerializer = new CharacterSerializer();
		this.stringSerializer = new StringSerializer();

		this.booleanArraySerializer = new BooleanArraySerializer();
		this.byteArraySerializer = new ByteArraySerializer();
		this.shortArraySerializer = new ShortArraySerializer();
		this.integerArraySerializer = new IntegerArraySerializer();
		this.longArraySerializer = new LongArraySerializer();
		this.floatArraySerializer = new FloatArraySerializer();
		this.doubleArraySerializer = new DoubleArraySerializer();
		this.characterArraySerializer = new CharacterArraySerializer();
		this.stringArraySerializer = new StringArraySerializer();

		this.classSerializer = new ClassSerializer();
		this.stringValueSerializer = new StringValueSerializer();

	}

	@Override
	protected void initDispatcher() {
		dispatcher.put(String.class, this.stringSerializer);
		dispatcher.put(Character.class, this.characterSerializer);
		dispatcher.put(Boolean.class, this.booleanSerializer);
		dispatcher.put(Integer.class, this.integerSerializer);
		dispatcher.put(Byte.class, this.byteSerializer);
		dispatcher.put(Double.class, this.doubleSerliazer);
		dispatcher.put(Float.class, this.floatSerializer);
		dispatcher.put(Short.class, this.shortSerializer);
		dispatcher.put(Long.class, this.longSerializer);

		dispatcher.put(String[].class, this.stringArraySerializer);
		dispatcher.put(int[].class, this.integerArraySerializer);
		dispatcher.put(byte[].class, this.byteArraySerializer);
		dispatcher.put(char[].class, this.characterArraySerializer);
		dispatcher.put(boolean[].class, this.booleanArraySerializer);
		dispatcher.put(long[].class, this.longArraySerializer);
		dispatcher.put(float[].class, this.floatArraySerializer);
		dispatcher.put(double[].class, this.doubleArraySerializer);
		dispatcher.put(short[].class, this.shortArraySerializer);

		dispatcher.put(Class.class, this.classSerializer);
		dispatcher.put(BigDecimal.class, this.stringValueSerializer);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.xmlro.SerializerFactory#getSerializer(java.lang.Class)
	 */
	public Serializer getSerializer(Class cl) {
		if (Map.class.isAssignableFrom(cl))
			return mapSerializer;
		if (Collection.class.isAssignableFrom(cl))
			return collectionSerializer;
		if (java.util.Date.class.isAssignableFrom(cl)) {
			return utcDateSerializer;
		}
		Serializer se = dispatcher.get(cl);
		if (se == null) {
			if (cl.isArray()) {
				se = this.objectArraySerializer;
			} else {
				// default serializer
				se = this.objectSerializer;
			}
		}
		return se;
	}
}
